package top.anka1.fxgui;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import top.anka1.ims.*;
import top.anka1.ims.RestaurantManagementSystem.Meal;

import java.util.ArrayList;
/**
 * AddMealToMenuController is a controller class for the Add Meal to Menu GUI.
 * It handles the interaction between the GUI and the underlying data model.
 * @author Anka
 */
public class AddMealToMenuController {
    RestaurantGUI restaurantGUI;
    Stage stage;
    ArrayList<Meal> mealsToAdd = new ArrayList<>();
    @FXML
    private TableView<Meal> mealsTable;
    @FXML
    private TableColumn<Meal, String> mealNameColumn;
    @FXML
    private TableColumn<Meal, String> mealIngredientsColumn;
    @FXML
    private TableView<Meal> menuTable;
    @FXML
    private TableColumn<Meal, String> menuNameColumn;
    @FXML
    private TableColumn<Meal, String> menuIngredientsColumn;

    public void showError(Stage stage, String title, String HeaderText, String contentText) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.initOwner(stage);
        alert.setTitle(title);
        alert.setHeaderText(HeaderText);
        alert.setContentText(contentText);
        alert.showAndWait();
    }
    public void showInformation(Stage stage, String title, String HeaderText, String contentText) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.initOwner(stage);
        alert.setTitle(title);
        alert.setHeaderText(HeaderText);
        alert.setContentText(contentText);
        alert.showAndWait();
    }
    public void setData(Stage stage, RestaurantGUI restaurantGUI) {
        this.restaurantGUI = restaurantGUI;
        this.stage = stage;
        mealNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        mealNameColumn.setStyle("-fx-alignment: CENTER;");
        mealIngredientsColumn.setCellValueFactory(new PropertyValueFactory<>("ingredientsString"));
        mealIngredientsColumn.setStyle("-fx-alignment: CENTER;");

        menuNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        menuNameColumn.setStyle("-fx-alignment: CENTER;");
        menuIngredientsColumn.setCellValueFactory(new PropertyValueFactory<>("ingredientsString"));
        menuIngredientsColumn.setStyle("-fx-alignment: CENTER;");

        ArrayList<Meal> meals = restaurantGUI.getRestaurantService().getMeals();
        ObservableList<Meal> mealsList = FXCollections.observableArrayList(meals);
        mealsTable.setItems(mealsList);

        ArrayList<Meal> menu = restaurantGUI.getRestaurantService().getMenu();
        ObservableList<Meal> menuList = FXCollections.observableArrayList(menu);
        menuTable.setItems(menuList);
    }

    @FXML
    public void addToMenu() {
        Meal selectedMeal = mealsTable.getSelectionModel().getSelectedItem();
        if (selectedMeal != null) {
            mealsToAdd.add(selectedMeal);
            ObservableList<Meal> menuList = FXCollections.observableArrayList(mealsToAdd);
            menuTable.setItems(menuList);
        } else {
            showError((Stage) mealsTable.getScene().getWindow(), "Selection Error", "No Meal Selected", "Please select a meal to add to the menu.");
        }
    }
    @FXML
    public void removeFromMenu() {
        Meal selectedMeal = menuTable.getSelectionModel().getSelectedItem();
        if (selectedMeal != null) {
            mealsToAdd.remove(selectedMeal);
            ObservableList<Meal> menuList = FXCollections.observableArrayList(mealsToAdd);
            menuTable.setItems(menuList);
        } else {
            showError((Stage) menuTable.getScene().getWindow(), "Selection Error", "No Meal Selected", "Please select a meal to remove from the menu.");
        }
    }
    @FXML
    public void confirmMenu() {
        try {
            restaurantGUI.getRestaurantService().setMenu(mealsToAdd);
                showInformation((Stage) menuTable.getScene().getWindow(), "Success", "Menu updated!", "The menu has been updated successfully.");
        } catch (Exception e) {
            showError((Stage) menuTable.getScene().getWindow(), "Error", "Failed to add meal", "Error: " + e.getMessage());
            return;
        }
        stage.close();
    }
}
